<template>
  <div class="article">
    <el-card>
      <template #header>
        <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        </el-breadcrumb>
      </template>
      <el-form label-width="100px" :model="form">
        <!-- 单选框组 -->
        <el-form-item label="状态:">
          <el-radio-group v-model="form.status">
            <el-radio :label="null">全部</el-radio>
            <el-radio :label="0">草稿</el-radio>
            <el-radio :label="1">待审核</el-radio>
            <el-radio :label="2">审核通过</el-radio>
            <el-radio :label="3">审核失败</el-radio>
            <el-radio :label="4">已删除</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- 下拉框 -->
        <el-form-item label="频道:">
          <HmSelect :form="form"></HmSelect>
          <!-- <el-select placeholder="请选择" v-model="form.channel_id">
          <el-option
              v-for="item in channelsList"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            ></el-option>
          </el-select> -->
        </el-form-item>
        <!-- 日期组件 -->
        <el-form-item label="日期:">
          <el-date-picker
            v-model="form.picker"
            type="daterange"
            start-placeholder="开始日期"
            range-separator="至"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <!-- 筛选按钮 -->
        <el-form-item>
          <el-button type="primary" @click="submit">筛选</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 文章类别 -->
    <el-card>
      <template #header> 根据筛选条件共查询到 {{ total }} 条结果 </template>
      <el-table :data="list">
        <el-table-column label="封面">
          <template v-slot="scope">
            <img class="cover" :src="scope.row.cover.images[0]" alt="" />
          </template>
        </el-table-column>
        <el-table-column label="标题" prop="title"></el-table-column>
        <el-table-column label="状态" prop="status">
          <template v-slot="scope">
            <el-tag type="success">{{
              scope.row.status === 2 ? '审核通过' : '审核未通过'
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="发布时间" width="200">
          <template v-slot="{ row }">
            {{ row.pubdate }}
          </template>
        </el-table-column>
        <el-table-column label="阅读数" prop="read_count"></el-table-column>
        <el-table-column label="评论数" prop="comment_count"></el-table-column>
        <el-table-column label="点赞数" prop="like_count"></el-table-column>
        <el-table-column label="操作" width="200">
          <template v-slot="scope">
            <div>
              <el-button
                type="primary"
                icon="el-icon-edit"
                circle
                @click="editor(scope.row)"
              ></el-button>
              <el-button
                type="danger"
                icon="el-icon-delete"
                circle
                @click="del(scope.row)"
              ></el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="form.page"
        :page-sizes="[1, 10, 20, 30]"
        :page-size="form.per_page"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { mpArticles, mpArticlesDelete } from '@/api/index.js'
// 导入获取文章列表api
export default {
  data () {
    return {
      form: {
        picker: [],
        status: null,
        channel_id: '',
        begin_pubdate: '',
        end_pubdate: '',
        page: 1,
        per_page: 10
      },
      list: [],
      total: 0
    }
  },
  async created () {
    this.submit()
  },
  watch: {
    'form.picker': {
      handler (newVal) {
        if (newVal.length === 2) {
          this.form.begin_pubdate = newVal[0]
          this.form.end_pubdate = newVal[1]
        }
      },
      deep: true
    }
  },
  methods: {
    async getData () {
      const res = await mpArticles(this.form)
      this.list = res.data.results
      this.total = res.data.total_count
      // console.log(res)
    },
    async submit () {
      // 点击搜索时回到第一页
      this.form.page = 1
      this.getData()
    },
    // 页码修改
    handleCurrentChange (page) {
      this.form.page = page
      this.getData()
    },
    // 页容量修改 页容量改变回到第一页
    handleSizeChange (size) {
      this.form.per_page = size
      this.form.page = 1
      this.getData()
    },
    // 删除
    del (row) {
      this.$confirm('您确定删除这条数据吗？', '温馨提示', {
        type: 'warning',
        confirmButtonText: '一定要删除',
        cancelButtonText: '点错了'
      })
        .then(async () => {
          console.log('确定')
          await mpArticlesDelete(row.id)
          this.$message.success('删除成功')
          // 刷新页面回到第一页
          this.submit()
        })
        .catch(() => {
          console.log('取消')
        })
    },
    // 编辑文章
    editor (row) {
      this.$router.push('/publish?id=' + row.id)
    }
  }
}
</script>

<style lang="less" scoped>
::v-deep .el-date-editor .el-range-separator {
  width: 10%;
}
.cover {
  width: 50px;
}
</style>
